<?php defined('BASEPATH') || exit('No direct script access allowed');

/**
 *  controller 仅作为小型活动处理
 */
class Mapsearch extends Front_Controller
{

    /**
     * Constructor
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
        $this->load->model('area/area_model');
        $this->load->model('Mapdata_model','mapdata_model');
    }

    /**
     * [fangJinSuo 地图检索]
     * @author [fengchen]
     * @DateTime 2017-02-23T14:20:10+0800
     * @return   [type]                   [description]
     */
    public function index() {
        
        //国家
        $country = $this->area_model->find_all_by("parent_id=0");
        // 省份
        $provinces = $this->area_model->provinces();
        // 写字楼数据
        // $datas = $this->mapdata_model->find_all();
        // dump($datas);exit();
        $data['country'] = $country;
        // $data['datas'] = $datas;
        $data['provinces'] = $provinces;
        $this->load->view('activity/map', $data);
    }
    public function xiezilou(){

        $this->load->view('activity/mapdata', []);
    }
    public function getdata(){
        $datas = $this->mapdata_model->find_all();
        if($datas){
            echo json_encode($datas);exit();
        }
    }
    /**
     * [ajax_area_data 省市区联动]
     * @return [type] [description]
     */
    public function ajax_area_data() {
        $parentID = $this->input->get('p_id');
        $data = $this->area_model->provinces($parentID);
        $html = '<option selected="selected" value="0">可选择</option>';
        if (!empty($data)) {
            foreach ($data as $obj) {
                $html .= '<option value="'.$obj->area_id.'">'.$obj->area_name.'</option>';
            }
        }
        echo $html;
    }
    /**
     * 获取经纬度新(因旧接口调取上海经纬度出bug)
     * @author fengchen@usuretech.com
     * @createtime 2016.11.7
     */
    public function getLocationNew(){
        $this->load->library( 'curl');
        $get = $this->input->get();
        $address = $get["address"];
        $getData['address'] = $address;
        $getData['output'] = 'json';
        $getData['ak'] = '8526cdc9eaa7faa35706d8d0fe54528d';

        $get_url = 'http://api.map.baidu.com/geocoder/v2/?'.http_build_query($getData, NULL, '&');
        $returnStr = $this->curl->_simple_call('get',$get_url);
        $returnData = json_decode($returnStr, true);
        if($returnData["result"]["location"]["lat"]!=""){
            $msg = "经纬度获取成功 （地址类型:".$returnData["result"]["level"]."）";
            $arr["lat"]=$returnData["result"]["location"]["lat"];
            $arr["lng"]=$returnData["result"]["location"]["lng"];
            echo json_encode(array('status' => '1', 'msg' => $msg, 'arr' => $arr));
        }else{
            $msg = "经纬度获取失败，请确认你的输入后重新获取!";
            echo json_encode(array('status' => '2', 'msg' => $msg, 'arr' => $returnData));
        }
    }
    public function receive(){
        $post = $this->input->post();
        if(!empty($post['data'])){
            // dump($post['data']);
            $data = json_decode($post['data']);
            foreach ($data  as $key => $value) {
                $this->mapdata_model->insert($value);
            }
            exit();
        }
    }
    public function handledata(){
        $head = ['名称','地址','电话','经度','纬度'];
        $res = $this->mapdata_model->find_all();
        $exports = [];
        if($res){
            foreach ($res as $key => $value) {
                $exports[$key]['title'] = $value->title;
                $exports[$key]['address'] = $value->address;
                $exports[$key]['phoneNumber'] = $value->phoneNumber;
                $exports[$key]['lat'] = $value->lat;
                $exports[$key]['lng'] = $value->lng;
                $exports[$key]['detailUrl'] = $value->detailUrl;
            }
        }
        // dump($exports);exit();
        $filename = "地图POI数据".date('Ymd');

        $this->exporttocsv($exports,$filename,$head);
    }
    /**
     * 导出csv
     * @author fengchen@usuretech.com
     * @time 2016/10/11
     */
    private function exporttocsv($data,$filename,$head){
         
        $header = implode("\",\"",array_values($head));
        $header = "\"" .$header;
        $header .= "\"\r\n";
        $content = '';
        $content .= $header;
         
        header("Expires: ".gmdate("D, d M Y H:i:s")." GMT");
        header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
        header("X-DNS-Prefetch-Control: off");
        header("Cache-Control: private, no-cache, must-revalidate, post-check=0, pre-check=0");
        header("Pragma: no-cache");
    
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/force-download");
        header("Content-Disposition: attachment; filename=".iconv("UTF-8","GB2312//IGNORE",$filename).".csv");
    
        $content = iconv("UTF-8","GB2312//IGNORE",$content);
        echo $content;
        if($data){
            foreach($data as $val){
                $content = "";
                $new_arr = array();
                foreach ($val as $key => $value)
                {
                    if(empty($value)){
                        $value = " ";
                    }
                    array_push($new_arr, preg_replace("/\"/","\"\"",$value));
                }
                $line = implode("\",\"",$new_arr);
                $line = "\"" .$line;
                $line .= "\"\r\n";
    
                $content .= $line;
                $content = iconv("UTF-8","GB2312//IGNORE",$content);
                echo  $content;
            }
        }
        exit();
    }
   
}